package leetcode.jianzhioffer;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-11-11 23:40
 **/
public class JianZhiOffer66 {

    public int[] constructArr(int[] a) {
        int len = a.length;
        if (len == 0) {
            return a;
        }
        //构建左侧数组和右侧数组，使得b[i] = left[i] * right[i];
        int[] left = new int[len];
        int[] right = new int[len];
        left[0] = right[len - 1] = 1;
        for (int i = 1; i < len; i++) {
            left[i] = left[i - 1] * a[i - 1];
        }

        for (int i = len - 2; i >= 0; i--) {
            right[i] = right[i + 1] * a[i + 1];
        }
        //结果数组
        int[] b = new int[len];
        for (int i = 0; i < len; i++) {
            b[i] = left[i] * right[i];
        }
        return b;
    }

}
